<?php

namespace App\Http\Controllers\Backend;

use App\DataTables\ItemOrderDataTable;
use App\Http\Requests;
use App\Http\Requests\CreateItemOrderRequest;
use App\Http\Requests\UpdateItemOrderRequest;
use App\Repositories\ItemOrderRepository;
use Flash;
use App\Http\Controllers\AppBaseController;
use Response;

class ItemOrderController extends AppBaseController
{
    /** @var  ItemOrderRepository */
    private $itemOrderRepository;

    public function __construct(ItemOrderRepository $itemOrderRepo)
    {
        $this->itemOrderRepository = $itemOrderRepo;
    }

    /**
     * Display a listing of the ItemOrder.
     *
     * @param ItemOrderDataTable $itemOrderDataTable
     * @return Response
     */
    public function index(ItemOrderDataTable $itemOrderDataTable)
    {
        return $itemOrderDataTable->render('backend.item_orders.index');
    }

    /**
     * Show the form for creating a new ItemOrder.
     *
     * @return Response
     */
    public function create()
    {
        return view('backend.item_orders.create');
    }

    /**
     * Store a newly created ItemOrder in storage.
     *
     * @param CreateItemOrderRequest $request
     *
     * @return Response
     */
    public function store(CreateItemOrderRequest $request)
    {
        $input = $request->all();

        $itemOrder = $this->itemOrderRepository->create($input);

        Flash::success(trans('flash.controller.save_success', ['model' => 'Item Order']));

        return redirect(route('itemOrders.index'));
    }

    /**
     * Display the specified ItemOrder.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        $itemOrder = $this->itemOrderRepository->findWithoutFail($id);

        if (empty($itemOrder)) {
            Flash::error(trans('flash.controller.not_found', ['model' => 'Item Order']));

            return redirect(route('itemOrders.index'));
        }

        return view('backend.item_orders.show')->with('itemOrder', $itemOrder);
    }

    /**
     * Show the form for editing the specified ItemOrder.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function edit($id)
    {
        $itemOrder = $this->itemOrderRepository->findWithoutFail($id);

        if (empty($itemOrder)) {
            Flash::error(trans('flash.controller.not_found', ['model' => 'Item Order']));

            return redirect(route('itemOrders.index'));
        }

        return view('backend.item_orders.edit')->with('itemOrder', $itemOrder);
    }

    /**
     * Update the specified ItemOrder in storage.
     *
     * @param  int              $id
     * @param UpdateItemOrderRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateItemOrderRequest $request)
    {
        $itemOrder = $this->itemOrderRepository->findWithoutFail($id);

        if (empty($itemOrder)) {
            Flash::error(trans('flash.controller.not_found', ['model' => 'Item Order']));

            return redirect(route('itemOrders.index'));
        }

        $itemOrder = $this->itemOrderRepository->update($request->all(), $id);

        Flash::success(trans('flash.controller.update_success', ['model' => 'Item Order']));

        return redirect(route('itemOrders.index'));
    }

    /**
     * Remove the specified ItemOrder from storage.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        $itemOrder = $this->itemOrderRepository->findWithoutFail($id);

        if (empty($itemOrder)) {
            Flash::error(trans('flash.controller.not_found', ['model' => 'Item Order']));

            return redirect(route('itemOrders.index'));
        }

        $this->itemOrderRepository->delete($id);

        Flash::success(trans('flash.controller.delete_success', ['model' => 'Item Order']));

        return redirect(route('itemOrders.index'));
    }
}
